package com.tianlh.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import com.tianlh.entity.Book;
import com.tianlh.quartz.CountJobEntry;
import com.tianlh.quartz.JobHelper;
import com.tianlh.quartz.SchedulerManager;
import com.tianlh.service.BookService;
import com.tianlh.service.impl.redis.RedisServiceImpl;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author wb-tianlihui
 * @date 2017/11/7
 */
@Controller
@RequestMapping("/")
public class TestController {

    @Autowired
    private BookService service;

    @Autowired
    private SchedulerManager manager;

    @Autowired
    RedisServiceImpl jedis;

    @RequestMapping("/test1")
    @ResponseBody
    public String test(@RequestParam(value = "bookId") long id, HttpServletResponse response) {
        response.setContentType("text/html;charset=GBK");
        Book book = service.getBookById(id);
        return JSON.toJSONString(book);
    }

    @RequestMapping("/test")
    @ResponseBody
    public void test1(HttpServletRequest request, HttpServletResponse response) throws SchedulerException {
        Map<String, Object> data = new HashMap<>();
        data.put("count", "0");
        data.put("key", System.currentTimeMillis());
        JobHelper.addJob(manager.getScheduler(), System.currentTimeMillis()+"", System.currentTimeMillis()+"",
            "0/5 * * * * ?", CountJobEntry.class, data);
        /*try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        JobHelper.addJob(manager.getScheduler(), System.currentTimeMillis()+"", "0/3 * * * * ?");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        JobHelper.addJob(manager.getScheduler(), System.currentTimeMillis()+"", "0/2 * * * * ?");*/
    }

    @RequestMapping("/jedis")
    @ResponseBody
    public String testJedis(HttpServletRequest request, HttpServletResponse response) {
        return jedis.setString();
    }
}
